package liuh.com.ledlamp.act;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

import com.blankj.utilcode.utils.StringUtils;
import com.blankj.utilcode.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.builder.PostStringBuilder;

import java.util.ArrayList;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import liuh.com.ledlamp.R;
import liuh.com.ledlamp.adapter.NormalRecyclerViewAdapter;
import liuh.com.ledlamp.handle.DisHandle;
import liuh.com.ledlamp.handle.DisTitle;
import liuh.com.ledlamp.popup.InputPopup;
import liuh.com.ledlamp.popup.ScalePopup;
import liuh.com.ledlamp.util.FileKit;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Bind(R.id.my_toolbar)
    Toolbar toolbar;
    @Bind(R.id.recycler_view)
    RecyclerView mRecyclerView;
    //
    Context mContext;
    NormalRecyclerViewAdapter mRecyclerViewAdapter;
    ScalePopup scalePopup;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //NoActionBar
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        mContext = this;
        ButterKnife.bind(this);
        // 初始化toolbar
        initToolBar();
        // 初始化recyclerView
        initRecycler();
        initProgress();
    }

    /**
     * @note 初始化进度条
     * @author liuh
     */
    private void initProgress() {
        progressDialog = new ProgressDialog(mContext);
        progressDialog.setIndeterminate(true);
        // 返回键失效
        progressDialog.setCancelable(false);
        progressDialog.setMessage("操作中...");
    }

    /**
     * @note 初始化tooBar
     * @author liuh
     */
    private void initToolBar() {
        toolbar.setTitle(R.string.app_name);
        toolbar.setTitleTextColor(ContextCompat.getColor(mContext, R.color.white));
        setSupportActionBar(toolbar);
    }

    /**
     * @note 初始化recycler view
     * @author liuh
     */
    private void initRecycler() {
        mRecyclerViewAdapter = new NormalRecyclerViewAdapter(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));//这里用线性显示 类似于listview
        //mRecyclerView.setLayoutManager(new GridLayoutManager(this, 2));//这里用线性宫格显示 类似于grid view
        //mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, OrientationHelper.VERTICAL));//这里用线性宫格显示 类似于瀑布流
        mRecyclerView.setAdapter(mRecyclerViewAdapter);
        //
        String[] titles = new String[]{};
        try {
            ArrayList<String> mlist = (ArrayList<String>) new FileKit(mContext).readObject("title_list");
            if (mlist != null) {
                titles = mlist.toArray(new String[0]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mRecyclerViewAdapter.addTitles(titles);
        //
        mRecyclerViewAdapter.setOnItemClickLitener(new NormalRecyclerViewAdapter.OnItemClickLitener() {

            @Override
            public void onItemClick(View view, int position) {
                PostStringBuilder builder = OkHttpUtils
                        .postString().tag(this);
                try {
                    String wid = view.getTag().toString();
                    String title = mRecyclerViewAdapter.getTitles().get(position);
                    Map<String, String> disTitles = (Map<String, String>) new FileKit(mContext).readObject("dis_title");
                    //根据title 获取分区id，if=null(显示内容请求);if!=null(撤销内容请求)
                    if (StringUtils.isEmpty(wid)) {
                        // 获取闲置的分区id
                        wid = DisTitle.getInstance().getWidByWids(disTitles);
                        //从分区集合中获取闲置分区，if=null(没有闲置);if!=null(该分区显示内容)
                        if (!StringUtils.isEmpty(wid)) {
                            // 内容发布请求
                            new DisHandle(mContext, mRecyclerViewAdapter, position, progressDialog).disContent(builder, title, wid);
                        } else {
                            // 没有空置的分区可使用
                            ToastUtils.showLongToast(MainActivity.this, "没有空置的分区可使用!");
                        }
                    } else {
                        // 内容撤销请求
                        new DisHandle(mContext, mRecyclerViewAdapter, position, progressDialog).unDisContent(builder, wid);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //ToastUtils.showLongToast(MainActivity.this,position + " click");
            }

            @Override
            public void onItemLongClick(View view, int position) {
                PostStringBuilder builder = OkHttpUtils
                        .postString().tag(this);
                try {
                    String wid = view.getTag().toString();
                    if (!StringUtils.isEmpty(wid)) {
                        // 内容撤销请求
                        new DisHandle(mContext, mRecyclerViewAdapter, position, progressDialog).unDisContent(builder, wid);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // 长按(修改内容、删除内容弹出框)
                scalePopup = new ScalePopup(MainActivity.this, mRecyclerViewAdapter, position);
                scalePopup.showPopupWindow();
            }
        });
    }

    /**
     * @note 显示内容
     * @author liuh
     * */

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.add_menu) {
            // 添加内容
            InputPopup input_popup = new InputPopup(MainActivity.this, mRecyclerViewAdapter, -1);
            input_popup.showPopupWindow();
            /*InputDialog input_dialog= new InputDialog(MainActivity.this,mRecyclerViewAdapter,-1);
            input_dialog.showInputDialog();*/
            return true;
        } else if (id == R.id.yijian_menu) {
            // 已经反馈
            send1("13613430730","");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void send1(String number, String message){
        Uri uri = Uri.parse("smsto:" + number);
        Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
        sendIntent.putExtra("sms_body", message);
        startActivity(sendIntent);
    }

    @Override
    protected void onStop() {
        super.onStop();
        ButterKnife.unbind(this);
        OkHttpUtils.getInstance().cancelTag(this);
        if (scalePopup != null) {
            scalePopup.dismiss();
        }
    }
}
